<?php
    /**
     * Created by JetBrains PhpStorm.
     * User: xiahuiqin
     * Date: 13-3-25
     * Time: 上午17:37
     * To change this template use File | Settings | File Templates.
     */
    class Index extends MY_Controller
    {
        function __construct()
        {
            parent::__construct();
            
            $this->load->model("column_mdl", 'column');
            $this->load->model("common_mdl", 'common');
            $this->load->model("member_mdl", 'member');
            $this->load->model("baby_mdl", 'baby');
            $this->load->model("like_baby_mdl", 'like_baby');
            $this->load->model("comment_mdl", 'comment');
            $this->load->model("focus_mdl", 'focus');
            $this->load->model("adfocus_mdl", 'adfocus');
            $this->load->model("baby_type_mdl", 'baby_type');
            $this->load->model("free_apply_mdl", 'free_apply');
            $this->load->model("free_baby_mdl", 'free_baby');
            $this->load->model("life_news_mdl", 'life_news');
            
            $this->column_class = $this->common->get_list(array('sign' => 'column_class')) ? $this->common->get_list(array('sign' => 'column_class')) : array();
            $this->member_id    = $this->session->userdata('member_id');
            $this->member_name    = $this->session->userdata('member_name');
        }

        /**
         * 首页
         */
        function index(){
            $data = array();

            $data['adfocus'] = $this->adfocus->get_list('','','','addtime desc');  //广告焦点图
            $data['cloth_class'] = $this->common->get_list(array('sign'=>'cloth_class'),'10','','id desc');  //宝贝分类
            //$data['cloth_style'] = $this->common->get_list(array('sign'=>'cloth_style'),'5','','id desc');  //宝贝风格
            $data['member'] = $this->member->get_list('','','','last_login_time desc');  //以上次登录时间逆序排序,会员
            $data['free_apply'] = $this->free_baby->get_list(array('isapply'=>'1','all_num >' => '0','left_num >' => '0'),'8');  //首页显示免费申请试用品，8份。
            $data['column_class'] = $this->column->get_list('','3','','updatetime desc');
            //首页显示10条新闻资讯
            $data['news_info'] = $this->life_news->get_list(array('sign'=>'news_info','isindex'=>'1'),'5','','updatetime desc');  
            $data['news_info2'] = $this->life_news->get_list(array('sign'=>'news_info','isindex'=>'1'),'5','5','updatetime desc');
			//首页显示10条生活私搭
            //$data['life_share'] = $this->life_news->get_list(array('sign'=>'life_share','isindex'=>'1'),'10','','updatetime desc');
            $data['topic'] = $this->life_news->get_list(array('ishot'=>'1','isindex'=>'1'),'5','','updatetime desc');
            
            $this->load->view('index',$data);
        }
        
        
        /**
         * 生活私搭+新闻资讯 列表页
         */
        function life_newsl($sign='news_info',$member_id=''){
        	$data = array();
        	$life_newsl = array();
        	$member = array();
        	
        	if(!empty($member_id)){
        		$life_newsl = $this->life_news->get_list(array('sign' => $sign,'member_id' => $member_id),'','','updatetime desc');  //以上次更新时间排序       列表页
        	}else{
        		$life_newsl = $this->life_news->get_list(array('sign' => $sign),'','','updatetime desc');  //以上次更新时间排序       列表页
        	}
        	
        	if(!empty($life_newsl)){
        		foreach($life_newsl as $lnk => $lnv){
        			$member[$lnk] = $this->member->get_one_list($lnv['member_id']);  //上传者信息
        		}
        	}
        	
        	$data['life_newsl'] = $life_newsl;
        	$data['member'] = $member;
        	$this->load->view('life_news/life_news_listV',$data);
        }
        
        /**
         * 生活私搭，新闻资讯内页详情
         */
        function life_news_detail($id=''){
        	$life_news = array();
        	$data = array();
        	        	 
        	$life_news = $this->life_news->get_one_list($id);
        	 
        	 
        	if(count($life_news) < 1){
        		redirect('index/life_newsl');  //如果id不存在，则跑列表页
        	}else{
        		$data['life_news'] = $life_news;   //该id对应的所有信息
        		
        		$data['left_news'] = $this->life_news->get_list(array('id !='=>$id,'sign'=>'news_info'),10,'','updatetime desc');  //除了该id以外的所有其他记录
        		
        		$before_news = $this->life_news->get_list(array('id <'=>$id,'sign' => $life_news['sign']),'','','id desc');
        		if(count($before_news) > 0){
        			$data['before_news'] = $before_news[0];
        		}
        		
        		$next_news = $this->life_news->get_list(array('id >'=>$id,'sign' => $life_news['sign']),'','','id asc');
        		if(count($next_news) > 0){
        			$data['next_news'] = $next_news[0];
        		}
        		
        		$this->load->view('life_news/life_newsV',$data);
        	}
        }
        
        
        /**
         * 免费申请所有列表页 
         */
        function free(){
        	$data = array();
        	$free_apply = array();
        	$apply_info = array();
        	$applyer = array();
        	
        	$free_apply = $this->free_baby->get_list(array('isapply'=>'1','all_num >' => '0','left_num >' => '0'),'','','updatetime desc');  //以上次更新时间排序,免费申请列表页
        	
        	if(!empty($free_apply)){
        		foreach($free_apply as $fak => $fav){
        			$apply_info[$fak] = $this->free_apply->get_list(array('baby_id'=>$fav['id']));  //申请单个宝贝的所有申请者填写的信息
        			
        			if(!empty($apply_info[$fak])) {
        				foreach($apply_info[$fak] as $aik => $aiv){
        					$applyer[$fak][$aik] = $this->member->get_one_list($aiv['applyer_id']);   //申请者个人头像，资料等信息
        				}
        			}
        		}
        		      		
        	}
        	
        	
        	$data['free_apply'] = $free_apply;
        	$data['apply_info'] = $apply_info;
        	$data['applyer'] = $applyer;
        	
        	$this->load->view('baby/baby_apply_listV',$data);
        }

        /**
         * 免费申请试用品内页详情
         */
        function free_detail($id=''){
        	$isfree = array();
        	$free_baby = array();
        	$applyer = array();
        	$apply_info = array();
        	
        	$isfree = $this->free_baby->get_one_list($id);
        	
        	
        	if($isfree['left_num'] < 1 || $isfree['all_num'] < 1){
        		redirect('index/free');  //如果这商品剩余申请数为0或者是总申请数为0，则跑到试用品列表页
        	}else{
        		
        		$free_baby = $this->free_baby->get_list(array('isapply'=>'1','all_num >' => '0','left_num >' => '0','id !=' => $id),'10','','updatetime desc');  //以上次更新时间排序,免费申请列表页
        		 
        		$apply_info = $this->free_apply->get_list(array('baby_id'=>$id));  //申请单个宝贝的所有申请者填写的信息
        		
        		if(!empty($apply_info)){
        			foreach($apply_info as $ak => $av){
        				$applyer[$ak] = $this->member->get_one_list($av['applyer_id']);   //申请者个人头像，资料等信息
        			}
        		}
        		
        		$data['isfree'] = $isfree;       		
        		$data['free_baby'] = $free_baby;  //除了这件商品的其他10件试用商品列表
        		$data['applyer_num'] = count($apply_info);  //申请这件商品的人数
        		$data['apply_info'] = $apply_info;  //申请者填写的申请信息
        		$data['applyer'] = $applyer;   //申请这件商品的所有人的个人资料（头像，等）
        		$this->load->view('baby/baby_applyV',$data);
        	}
        }
        
        /**
         * 加关注
         */
        function addfocus(){
        	$data['info'] = '';
        	$focuser = $this->input->post('focuser');
        	
        	if (empty($this->member_id)){
        		$data = array('info'=>'请先登录!');
        	}
        	if(empty($focuser)){
        		$data = array('info'=>'未选择关注对象');
        	}
            if($focuser && !empty($this->member_id)){
                $datas['focuser'] = $this->member_id;
                $datas['focuseder'] = $this->input->post('focuser');
                $datas['addtime'] = date("Y-m-d H:i:s");

                $check = $this->focus->get_list(array('focuser'=>$this->member_id,'focuseder'=>$this->input->post('focuser')));
                if(empty($check)){
                    if($this->focus->insert($datas)){
                        $data = array('info'=>'关注成功~');
                    }else{
                        $data = array('info'=>'服务器繁忙~');
                    }
                }else{
                	$data = array('info'=>'您已关注过这位格格.');
                }
            }
            
            echo json_encode($data);
        }

        /**
         * 所有产品页
         */
        function baby_list($id = '',$style_id=''){  //女装
            if(!empty($id) && ($id != 'style') && ($id != 'sonclass')){
                $data['baby'] = $this->baby->get_list(array('baby_class_id'=>$id),'','','addtime desc');  //宝贝分类
            }elseif(!empty($style_id) && $id == 'style'){
                $data['baby'] = $this->baby->get_list(array('style_id'=>$style_id),'','','addtime desc');  //宝贝风格
            }elseif(!empty($style_id) && ($id == 'sonclass')){
                $data['baby'] = $this->baby->get_list(array('baby_sonclass_id'=>$style_id),'','','addtime desc');  //宝贝子分类
            }

            $data['cloth_style'] = $this->common->get_list(array('sign'=>'cloth_style'));
            $data['cloth_class'] = $this->common->get_list(array('sign'=>'cloth_class'),'3');  //服装分类
            $this->load->view('baby/baby_listV',$data);
        }
        
        /**
         * 宝贝详情页
         */
        function baby_detail($id=''){
        	$id = is_numeric($this->uri->segment(3)) ? $this->uri->segment(3) : $id;
        	//$id = $id ? $id : $this->uri->segment(3);

        	$one_baby = $this->baby->get_one_list($id);
        	$data['one_baby'] = $one_baby;
        	$like_latest = $this->like_baby->get_list(array('baby_id'=>$one_baby['id']),1,'','addtime desc');

        	if(!empty($like_latest)){
                $data['liker_latest'] = $this->member->get_one_list($like_latest[0]['liker_id']);
                $data['like_detail'] = $like_latest[0];
            }else{
                $data['liker_latest'] = array();
                $data['like_detail'] = array();
            }


        	$data['comment'] = $this->comment->get_list(array('baby_id'=>$one_baby['id']),'','','addtime desc');
        	$this->load->view('baby/babyV',$data);
        }

        /**
         * 添加对商品的评论
         */
        function comment_add(){
        	if($this->input->post('baby_id')){
        		$tmp['baby_id'] = $this->input->post('baby_id');
        		$tmp['member_id'] = $this->member_id;
        		$tmp['desc'] = $this->input->post('desc');
        		$tmp['addtime'] = date("Y-m-d H:i:s");
        		 
        		$this->comment->insert($tmp);
        		$url = front_url('index/baby_detail').'/'.$this->input->post('baby_id');
        		
        		redirect($url);
        	}else{
        		$this->index();
        	}
        }
        
        /**
         * 金币兑换
         */
        function gold_exchange(){
        	$this->load->view('gold_exchange');
        }
        
        /**
         * 申请试用
         */
        function apply_commit($member_id=''){
        	$data = array();
        	$datas = array();
        	
        	$datas['name']  = $this->input->post('name'); //姓名
        	$datas['phone'] = $this->input->post('phone'); //联系电话
        	$datas['postcode'] = $this->input->post('postcode'); //邮编
        	$datas['comment']   = $this->input->post('comment'); //申请理由  	
        	$datas['address']    = $this->input->post('address'); //地址
        	$id = $this->input->post('id');  //baby_id
        	
        	//如果是登录状态用户
        	if(!empty($member_id) && ($member_id == $this->member_id)){
        		$apply_before = $this->free_apply->get_list(array('baby_id'=>$id,'applyer_id'=>$this->member_id));  //验证此用户之前有没有申请过该商品
        		if(count($apply_before) > 0){
        			$data = array('error'=>'小主，您已经申请过该商品啦~','status'=>'0');
        		}else{
        			$datas['applyer_ip'] = ip2long($this->input->ip_address());
        			$datas['baby_id'] = $id;
        			$datas['applyer_id'] = $member_id;
        			$datas['applytime'] = date('Y-m-d H:i:s');
        			
        			if($this->free_apply->insert($datas)){
        				$data = array('error'=>'','status'=>'1');
        			}else{
        				$data = array('error'=>'现在申请人数过多，稍后再来重试吧~','status'=>'0');
        			}
        		}
        	}else{
        		$data = array('error'=>'小主，请先登录了再来申请试用吧~','status'=>'0');
        	}
        	
        	echo json_encode($data);
        }
        
     
    }
